package com.qq.BFMRSE.frame;

import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.qq.BFMRSE.entity.loginState;
import com.qq.BFMRSE.requestModule.loginModule;
import com.qq.BFMRSE.util.BFConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

public class loginFrame extends JFrame implements MouseListener{
	private static final long serialVersionUID = 1L;
	private JLabel titleLable;
	private JLabel nameLable;
	private JLabel pwdLable;
	private static JTextField loginName;
	private static JPasswordField pwd ;
	private JButton loginBut ;
	private JButton exitBut;
	private Boolean isLogin;
	private JLabel findPwdLable;
	private JLabel registerLable;
	
	public  loginFrame() {
		init();
		this.setTitle("BFMRSE登录界面");
        ImageIcon imgBF=new ImageIcon("images\\BFMRSE.png");
        this.setIconImage(imgBF.getImage()); 
		getContentPane().setLayout(null);
		setSize(500, 300);
		setVisible(true);
		setResizable(true);// 禁止拉边框拉长拉断
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//点击退出
		exitBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		//点击登录
		loginBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=loginName.getText();
				String password=String.valueOf(pwd.getPassword());
				if("".equals(name)||"".equals(password)){
					JOptionPane.showMessageDialog(null, "帐号或密码不能为空！");
				}else{
					loginModule logConf=new loginModule(name, password);
					if(logConf.Login()){
						dispose();//关闭登录界面
						loginState.isLogin=true;
						loginState.loginName=name;//记录登录状态
						mainFrame mainfa=new mainFrame(name);//登录成功进入主界面
						mainfa.setVisible(true);
						File mainFile=new File(name);
						if(!mainFile.exists()){
							mainFile.mkdir();
						}
						BFConstants.WORDINFOFILE=name+File.separator+BFConstants.WORDINFOFILE;
						File wordInfoFile=new File(BFConstants.WORDINFOFILE);
						if(!wordInfoFile.exists()){
							wordInfoFile.mkdir();
						}
						BFConstants.INDEXPATH=name+File.separator+BFConstants.INDEXPATH;
						File indexFile=new File(BFConstants.INDEXPATH);
						if(!indexFile.exists()){
							indexFile.mkdir();
						}
					}else{
						JOptionPane.showMessageDialog(null, "帐号或密码不正确！",
								"提示",JOptionPane.INFORMATION_MESSAGE);
						//JOptionPane.showMessageDialog(null, "帐号或密码不正确！");
					}			
				}
			}
		});
		registerLable.addMouseListener(this);
	}
	public void init(){
		titleLable=new JLabel("帐号登陆");
		nameLable = new JLabel("帐号");
		pwdLable= new JLabel("密码");
		loginName = new JTextField(10);
		pwd = new JPasswordField(10);
		loginBut = new JButton("登录");
		exitBut = new JButton("退出");
		findPwdLable= new JLabel("找回密码");
		registerLable=new JLabel("注册");
		isLogin=false;
		titleLable.setBounds(199, 20, 106, 30);
		getContentPane().add(titleLable);
		nameLable.setBounds(163, 72, 30, 30);
		getContentPane().add(nameLable);
		pwdLable.setBounds(163, 122, 30, 30);
		getContentPane().add(pwdLable);
		loginName.setBounds(218, 74, 100, 25);
		getContentPane().add(loginName);
		pwd.setBounds(218, 125, 100, 25);
		getContentPane().add(pwd);
		loginBut.setBounds(268, 172, 65, 30);
		getContentPane().add(loginBut);
		exitBut.setBounds(191, 172, 65, 30);
		getContentPane().add(exitBut);
		findPwdLable.setBounds(9, 249, 61, 17);
		getContentPane().add(findPwdLable);
		registerLable.setBounds(439, 249, 61, 17);
		getContentPane().add(registerLable);
		
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					loginFrame frame = new loginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		//点击鼠标释放后的相应
		if(e.getSource()==registerLable){
			registerFrame reFrame=new registerFrame();
			reFrame.setVisible(true);
			reFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}